package edu.gatech.cs2340.thecyclonesquad;

import edu.gatech.cs2340.thecyclonesquad.database.Database;
import edu.gatech.cs2340.thecyclonesquad.models.InMemoryModel;
import edu.gatech.cs2340.thecyclonesquad.models.Person;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class UnlockUserActivity extends ListActivity {
	
	public final static String UNLOCK_MESSAGE = "edu.gatech.cs2340.thecyclonesquad.USERNAMEJ";
	
	//InMemoryModel users = new InMemoryModel();
	//String lockedUsers[] = users.getLockedUsers();
	String lockedUsers[];
	
	
	@SuppressLint("NewApi")
	@Override
	/**
	 * Initializes our activity.
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_unlock_user);
		// Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        Database entry = new Database(this);
    	entry.open();
    	String lockedUsers[] = entry.getLockedOutUsers();
    	entry.close();
        
        //this function makes an array into a list, so lockedUsers is now a list
        setListAdapter(new ArrayAdapter<String>(UnlockUserActivity.this, android.R.layout.simple_list_item_1, lockedUsers));
	}
	
	/**
	 * Unlock the user clicked.
	 */
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
        Database entry = new Database(this);
    	entry.open();
    	String lockedUsers[] = entry.getLockedOutUsers();
		String dude = lockedUsers[position];
		entry.unlockUser(dude);
		//Person p = users.findPerson(dude);
		//p.locked = false;
		//p.loginAttempts = 0;
		entry.close();
		Intent intent = new Intent(this, UserNowUnlockedActivity.class);
		intent.putExtra(UNLOCK_MESSAGE, dude);
		startActivity(intent); 

	}

	@Override
	/**
	 * Initialize the contents of the Activity's standard options menu.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_unlock_user, menu);
		return true;
	}

}
